package com.shrct.businessprocess.project;

import com.shrct.businessprocess.organization.RCCEORoleProvider;
import com.shrct.businessprocess.organization.RCEmployee;
import com.shrct.businessprocess.organization.RCRoleAssignment;
import com.shrct.businessprocess.organization.RCSalesLeaderRoleProvider;
import com.shrct.businessprocess.organization.impl.RCEmployeeImpl;
import com.shrct.businessprocess.organization.impl.RCRoleAssignmentImpl;
import com.shrct.businessprocess.project.RCInitializeProjectProcess;
import com.shrct.businessprocess.project.RCProject;
import com.shrct.businessprocess.project.RCProjectDAO;
import com.shrct.businessprocess.project.RCProjectSaleAssignment;
import com.shrct.businessprocess.project.RCProjectStatus;
import com.shrct.businessprocess.project.impl.RCProjectImpl;
import edu.fudan.langlab.domain.task.Task;
import edu.fudan.langlab.domain.task.TaskService;
import edu.fudan.langlab.domain.workflow.BusinessProcess;
import edu.fudan.langlab.domain.workflow.ProcessManager;
import edu.fudan.langlab.domain.workflow.ProcessTask;
import edu.fudan.langlab.domain.workflow.Token;
import edu.fudan.mylang.pf.test.BaseTest;
import java.util.Collection;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;

@ContextConfiguration(locations = "classpath:HibernateApplicationContext-test.xml")
public class ProjectProcessTest extends BaseTest {
  public ProjectProcessTest() {
    super();
  }
  
  @Autowired
  private ProcessManager processManager;
  
  @Autowired
  private TaskService taskService;
  
  @Autowired
  private RCProjectDAO projectProcessService;
  
  @Autowired
  private RCSalesLeaderRoleProvider salesLeaderRoleProvider;
  
  @Autowired
  private RCCEORoleProvider ceoRoleProvider;
  
  public void initData() {RCEmployee rCEmployee=RCEmployeeImpl.create(getObjectFactory());
    final Procedure1<RCEmployee> _function = new Procedure1<RCEmployee>() {
        public void apply(final RCEmployee employee) {
          employee.setEid("00001");RCRoleAssignment rCRoleAssignment=RCRoleAssignmentImpl.create(getObjectFactory());
          final Procedure1<RCRoleAssignment> _function = new Procedure1<RCRoleAssignment>() {
              public void apply(final RCRoleAssignment it) {
                it.setEmployee(employee);
                String _roleId = ProjectProcessTest.this.salesLeaderRoleProvider.getRoleId();
                it.setRoleType(_roleId);
              }
            };
          ObjectExtensions.<RCRoleAssignment>operator_doubleArrow(rCRoleAssignment, _function);
        }
      };
    final RCEmployee salesLeader = ObjectExtensions.<RCEmployee>operator_doubleArrow(rCEmployee, _function);RCEmployee rCEmployee_1=RCEmployeeImpl.create(getObjectFactory());
    final Procedure1<RCEmployee> _function_1 = new Procedure1<RCEmployee>() {
        public void apply(final RCEmployee employee) {
          employee.setEid("00002");RCRoleAssignment rCRoleAssignment=RCRoleAssignmentImpl.create(getObjectFactory());
          final Procedure1<RCRoleAssignment> _function = new Procedure1<RCRoleAssignment>() {
              public void apply(final RCRoleAssignment it) {
                it.setEmployee(employee);
                String _roleId = ProjectProcessTest.this.ceoRoleProvider.getRoleId();
                it.setRoleType(_roleId);
              }
            };
          ObjectExtensions.<RCRoleAssignment>operator_doubleArrow(rCRoleAssignment, _function);
        }
      };
    final RCEmployee ceo = ObjectExtensions.<RCEmployee>operator_doubleArrow(rCEmployee_1, _function_1);
  }
  
  @Test
  public void testInitProjectNormalProcess() {
    this.initData();RCEmployee rCEmployee=RCEmployeeImpl.create(getObjectFactory());
    final RCEmployee saler = rCEmployee;RCProject rCProject=RCProjectImpl.create(getObjectFactory());
    final Procedure1<RCProject> _function = new Procedure1<RCProject>() {
        public void apply(final RCProject it) {
          it.setCode("00001");
          RCProjectSaleAssignment _createAndAddtoSalers = it.createAndAddtoSalers();
          final Procedure1<RCProjectSaleAssignment> _function = new Procedure1<RCProjectSaleAssignment>() {
              public void apply(final RCProjectSaleAssignment it) {
                it.setIsMajor(Boolean.valueOf(true));
                it.setEmployee(saler);
              }
            };
          ObjectExtensions.<RCProjectSaleAssignment>operator_doubleArrow(_createAndAddtoSalers, _function);
        }
      };
    final RCProject project = ObjectExtensions.<RCProject>operator_doubleArrow(rCProject, _function);
    this.projectProcessService.createInitializeProjectProcess(project);
    RCEmployee _party = this.salesLeaderRoleProvider.getParty();
    Collection<Task> _findTasks4Owner = this.taskService.findTasks4Owner(_party);
    int _size = _findTasks4Owner.size();
    Assert.assertEquals(1, _size);
    RCProjectStatus _status = project.getStatus();
    Assert.assertEquals(RCProjectStatus.auditing, _status);
    RCEmployee _party_1 = this.salesLeaderRoleProvider.getParty();
    Collection<Task> _findTasks4Owner_1 = this.taskService.findTasks4Owner(_party_1);
    final Function1<Task,Boolean> _function_1 = new Function1<Task,Boolean>() {
        public Boolean apply(final Task it) {
          return Boolean.valueOf(true);
        }
      };
    Task _findFirst = IterableExtensions.<Task>findFirst(_findTasks4Owner_1, _function_1);
    ProcessTask task = ((ProcessTask) _findFirst);
    String _description = task.getDescription();
    Assert.assertEquals("\u9879\u76EE\u7F16\u53F7:00001", _description);
    Token _token = task.getToken();
    BusinessProcess _bp = _token.getBp();
    RCInitializeProjectProcess p = ((RCInitializeProjectProcess) _bp);
    p.SalesManagerAudit_agree("Agree");
    RCEmployee _party_2 = this.ceoRoleProvider.getParty();
    Collection<Task> _findTasks4Owner_2 = this.taskService.findTasks4Owner(_party_2);
    int _size_1 = _findTasks4Owner_2.size();
    Assert.assertEquals(1, _size_1);
    RCEmployee _party_3 = this.ceoRoleProvider.getParty();
    Collection<Task> _findTasks4Owner_3 = this.taskService.findTasks4Owner(_party_3);
    final Function1<Task,Boolean> _function_2 = new Function1<Task,Boolean>() {
        public Boolean apply(final Task it) {
          return Boolean.valueOf(true);
        }
      };
    Task _findFirst_1 = IterableExtensions.<Task>findFirst(_findTasks4Owner_3, _function_2);
    task = ((ProcessTask) _findFirst_1);
    Token _token_1 = task.getToken();
    BusinessProcess _bp_1 = _token_1.getBp();
    p = ((RCInitializeProjectProcess) _bp_1);
    p.CEOAudit_agree("Agree");
    RCProjectStatus _status_1 = project.getStatus();
    Assert.assertEquals(RCProjectStatus.starting, _status_1);
  }
  
  @Test
  public void testInitProjectSalesLeaderRejectProcess() {
    this.initData();RCEmployee rCEmployee=RCEmployeeImpl.create(getObjectFactory());
    final RCEmployee saler = rCEmployee;RCProject rCProject=RCProjectImpl.create(getObjectFactory());
    final Procedure1<RCProject> _function = new Procedure1<RCProject>() {
        public void apply(final RCProject it) {
          it.setCode("00001");
          RCProjectSaleAssignment _createAndAddtoSalers = it.createAndAddtoSalers();
          final Procedure1<RCProjectSaleAssignment> _function = new Procedure1<RCProjectSaleAssignment>() {
              public void apply(final RCProjectSaleAssignment it) {
                it.setIsMajor(Boolean.valueOf(true));
                it.setEmployee(saler);
              }
            };
          ObjectExtensions.<RCProjectSaleAssignment>operator_doubleArrow(_createAndAddtoSalers, _function);
        }
      };
    final RCProject project = ObjectExtensions.<RCProject>operator_doubleArrow(rCProject, _function);
    final RCEmployee salesLeader = this.salesLeaderRoleProvider.getParty();
    final RCEmployee ceo = this.ceoRoleProvider.getParty();
    RCInitializeProjectProcess p = this.projectProcessService.createInitializeProjectProcess(project);
    p.SalesManagerAudit_reject("not valid");
    Collection<Task> _findTasks4Owner = this.taskService.findTasks4Owner(saler);
    final Function1<Task,Boolean> _function_1 = new Function1<Task,Boolean>() {
        public Boolean apply(final Task it) {
          return Boolean.valueOf(true);
        }
      };
    Task _findFirst = IterableExtensions.<Task>findFirst(_findTasks4Owner, _function_1);
    ProcessTask task = ((ProcessTask) _findFirst);
    Assert.assertNotNull(task);
    p.SalesModify_modified("modified");
    p.SalesManagerAudit_agree("OK");
    p.CEOAudit_reject("reject");
    Collection<Task> _findTasks4Owner_1 = this.taskService.findTasks4Owner(salesLeader);
    final Function1<Task,Boolean> _function_2 = new Function1<Task,Boolean>() {
        public Boolean apply(final Task it) {
          return Boolean.valueOf(true);
        }
      };
    Task _findFirst_1 = IterableExtensions.<Task>findFirst(_findTasks4Owner_1, _function_2);
    task = ((ProcessTask) _findFirst_1);
    Assert.assertNotNull(task);
    p.SalesManagerAudit_reject("ceo reject");
    Collection<Task> _findTasks4Owner_2 = this.taskService.findTasks4Owner(salesLeader);
    int _size = _findTasks4Owner_2.size();
    Assert.assertEquals(0, _size);
    Collection<Task> _findTasks4Owner_3 = this.taskService.findTasks4Owner(ceo);
    int _size_1 = _findTasks4Owner_3.size();
    Assert.assertEquals(0, _size_1);
    Collection<Task> _findTasks4Owner_4 = this.taskService.findTasks4Owner(saler);
    int _size_2 = _findTasks4Owner_4.size();
    Assert.assertEquals(1, _size_2);
    p.SalesModify_modified("modified again");
    p.SalesManagerAudit_agree("aggree again");
    p.CEOAudit_agree("final aggree");
    Collection<Task> _findTasks4Owner_5 = this.taskService.findTasks4Owner(salesLeader);
    int _size_3 = _findTasks4Owner_5.size();
    Assert.assertEquals(0, _size_3);
    Collection<Task> _findTasks4Owner_6 = this.taskService.findTasks4Owner(ceo);
    int _size_4 = _findTasks4Owner_6.size();
    Assert.assertEquals(0, _size_4);
    Collection<Task> _findTasks4Owner_7 = this.taskService.findTasks4Owner(saler);
    int _size_5 = _findTasks4Owner_7.size();
    Assert.assertEquals(0, _size_5);
  }
}
